﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using TEST_SCCM_MVC_01.Models;

namespace TEST_SCCM_MVC_01.Controllers
{
    public class TreeViewController : Controller
    {

        public ActionResult SCCMCollections(string RootCollectionID = "COLLROOT")
        {
            CollectionRepository ColRepo = new CollectionRepository();

            IEnumerable<TreeViewItemModel> nodes = from col in ColRepo.GetCollectionsByParent(RootCollectionID)
                        select new TreeViewItemModel
                        {
                            Text = col.Name,
                            Value = col.CollectionID,
                            LoadOnDemand = col.SubColCount > 0
                        };

            return View(nodes);
        }


        /// <summary>
        /// Renders a TreeView as a Partital View
        /// </summary>
        /// <param name="RootCollectionID">CoolectionID for TreeView Root</param>
        /// <returns>PartitalView</returns>
        [ChildActionOnly]
        public ActionResult PV_SCCMCollections(string RootCollectionID = "COLLROOT")
        {
            CollectionRepository ColRepo = new CollectionRepository();

            IEnumerable<TreeViewItemModel> nodes = from col in ColRepo.GetCollectionsByParent(RootCollectionID)
                                                   select new TreeViewItemModel
                                                   {
                                                       Text = col.Name,
                                                       Value = col.CollectionID,
                                                       LoadOnDemand = col.SubColCount > 0
                                                   };

            return PartialView(nodes);
        }

        /// <summary>
        /// Loads SubCollection List from Database
        /// </summary>
        /// <param name="node">A TreeViewItem passed from the ajax query</param>
        /// <returns>A TreeViewItemModel List</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult _SubCollectionLoading(TreeViewItem node)
        {
            CollectionRepository ColRepo = new CollectionRepository();

            if (String.IsNullOrEmpty(node.Value))
            {
                throw new Exception("Node value can not be empty or null! \r\n Please also check Collection IDs in Config!");
            }

            IEnumerable<TreeViewItemModel> nodes = from s in ColRepo.GetCollectionsByParent(node.Value)
                        select new TreeViewItemModel
                        {
                            Text = s.Name,
                            Value = s.CollectionID,
                            LoadOnDemand = s.SubColCount > 0
                        };

            return new JsonResult { Data = nodes };
        }


        // *********************************************************************************************************


        //
        // GET: /TreeView/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /TreeView/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /TreeView/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /TreeView/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /TreeView/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /TreeView/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TreeView/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TreeView/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
